<?php
/* Web Statistics and Analytics Console (PHP)
 *  subpages/generate/generate.php
 *  Author: Trevor Bidyk
 *  Created: January 1, 2000
 *  Last modified: April 4, 2011
 *   (C) Us 2011
 *   Uses Simple PHP Framework, licensed under the FreeBSD license.
 *    View license in ./include/LICENSE.
 *   All rights reserved.
 */
if (isset($_POST['SubmitButton']) && $_POST['SubmitButton']) {
    $db = Database::getDatabase();
    //
    // Ultra important!! This should stop really stupid users from attempting very
    // large database operations. Most scripts should default to thirty seconds,
    //  but some hosts are really stupid
    //
    @set_time_limit(60);

    $_POST['SessionCount'] = (int) $_POST['SessionCount'];
    $SessionCount = (is_int($_POST['SessionCount']) ? $_POST['SessionCount'] : 100);
    $_POST['EventCount'] = (int) $_POST['EventCount'];
    $EventCount = (is_int($_POST['EventCount']) ? $_POST['EventCount'] : 25);

    $Result;
    $SessionsAffected = 0;
    $EventsAffected = 0;
    $ActualEventCount = 0;

    for ($i = 0; $i < $SessionCount; $i++) {
        $IP = GenerateIP();
        $Date = GenerateDate(86400);
        $TokenID = GenerateString(32);
        $UserAgent = GenerateUserAgent();

        $Result = $db->query("INSERT INTO Event_Session (IP, Session_Time, Token_ID, User_Agent) VALUES ('$IP', '$Date', '$TokenID', '$UserAgent')");
        if ($db->affectedRows($Result) == 1) {
            $SessionsAffected++;
        } else {
            echo 'Unable to insert session into session table.';
            exit;
        }
        $ActualEventCount = rand(1, $EventCount);

        for ($j = 0; $j < $ActualEventCount; $j++) {
            $EventType = GenerateEventType();
            $Secure = 0;    // We don't implement secure http right meow.
            $HostName = 'generatedata.com';
            $Parameters = GenerateParameters();
            $URL = "http://$HostName$Parameters";
            $Payload = 'Data generated in PHP Admin Console';
            $Reference = GenerateReference();

            $Result = $db->query("CALL InsertEvent('$EventType', '$TokenID', $Secure, '$HostName', '$URL', '$Parameters', '$Payload', '$Date', '$Reference')");
            if ($db->affectedRows($Result) == 1) {
                $EventsAffected++;
            } else {
                echo 'Unable to call InsertEvent() procedure.';
                exit;
            }
        }
    }
}
?>
<div style="margin: 20px">
    <h2>Generate Test Data</h2>
    <span>Select the number of sessions to generate in the database. The generated amount of events in a session is extremely approximate, as a random value up the the maximum will be used. No not attempt to create more than 100,000 events at once.</span><br />
    <br />
    <?php
    if (isset($_POST['SubmitButton']) && $_POST['SubmitButton']) {
        echo '<div id="statspanel">';
        echo '<h4>Data Generation Result:</h4>';
        echo "<span><em>Sessions: </em>$SessionsAffected</span><br />";
        echo "<span><em>Events: </em>$EventsAffected</span>";
        echo '</div>';
    }
    ?>

    <form action="./generate_data.php?do=generate" method="post">
        <label for="SessionCount">Number of unique sessions:</label><br />
        <input type="text" id="SessionCount" name="SessionCount" value="100" /><br />

        <label for="EventCount">Maximum number of events per session:</label><br />
        <input type="text" id="EventCount" name="EventCount" value="25" /><br />
        <br />
        <input type="submit" name="SubmitButton" value="Submit" />
    </form>
</div>
